package com.pur.zsms.supplier.service.impl;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelBlPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.supplier.dao.SupplierOrderDao;

public class ExcelInMaterValue extends Thread {

	private final int i;
	private final Map<Integer, String> map;
	private final List<String> userIdList;

	private SupplierOrderDao supplierOrderDao;

	private IMaterDao materDaoImpl;

	private ISelectFoodDao selectFoodDaoImpl;

	public ExcelInMaterValue(int i, Map<Integer, String> map, List<String> userIdList,
	        SupplierOrderDao supplierOrderDao, IMaterDao materDaoImpl, ISelectFoodDao selectFoodDaoImpl) {
		this.i = i;
		this.map = map;
		this.userIdList = userIdList;
		this.supplierOrderDao = supplierOrderDao;
		this.materDaoImpl = materDaoImpl;
		this.selectFoodDaoImpl = selectFoodDaoImpl;

	}

	@Override
	public void run() {
		try {
			String[] everyline = map.get(i).split("\\*");

			int materId = Integer.valueOf(new BigDecimal(everyline[0]).intValue());
			int materLevel = Integer.valueOf(new BigDecimal(everyline[1]).intValue());
			int supId = Integer.valueOf(new BigDecimal(everyline[2]).intValue());
			String materDay = everyline[4];
			BigDecimal materValueUndressed = new BigDecimal(everyline[5]);

			// 取出现有系统中的材料

			for (String userId : userIdList) {
				// 校验用户是否订单日 订购此种材料
				List<OrderSupplierPo> orderSupplierPolist = supplierOrderDao.isExistOrderSup(userId,
				        String.valueOf(supId), materDay, materId);
				if (orderSupplierPolist == null || orderSupplierPolist.size() == 0) {
					continue;
				}

				MaterLevelPo materLevelPo = new MaterLevelPo();

				/*// 计算净料价格
				MathContext mc = new MathContext(5, RoundingMode.HALF_UP);
				BigDecimal materValueNet = null;

				List<MaterLevelPo> materLevelPoList = materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId(
				        materId, materLevel, supId, userId);
				if (materLevelPoList == null || materLevelPoList.size() == 0) {
					MaterLevelPo materLevelPoOther = materDaoImpl.getMaterLevelPoByMaterId(materId).get(0);

					BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelPo.class, false);
					copier.copy(materLevelPoOther, materLevelPo, null);
					materLevelPo.setMid(0);
					materLevelPo.setSupId(supId);
					materLevelPo.setCustId(Integer.valueOf(userId));

					// 1替换当前材料表中 的价格
					materValueNet = materValueUndressed.multiply(new BigDecimal(0.1))
					        .divide(new BigDecimal(materLevelPo.getMaterProportion()), mc)
					        .setScale(5, BigDecimal.ROUND_HALF_UP);
					// 改变po
					materLevelPo.setMaterValue(materValueNet);
					materLevelPo.setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));

					materDaoImpl.addMaterLevelPersist(materLevelPo);
				} else {
					materLevelPo = materLevelPoList.get(0);
					// 1替换当前材料表中 的价格
					materValueNet = materValueUndressed.multiply(new BigDecimal(0.1))
					        .divide(new BigDecimal(materLevelPo.getMaterProportion()), mc)
					        .setScale(5, BigDecimal.ROUND_HALF_UP);
					// 改变po
					materLevelPo.setMaterValue(materValueNet);
					materLevelPo.setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));
					materDaoImpl.addMaterLevel(materLevelPo);
				}

				// 2.插入材料备份表中
				List<MaterLevelBlPo> materLevelBlPoList = materDaoImpl
				        .getMaterLevelBlPoByMaterIdAndLevelAndSupId(materId, materLevel, supId, materDay,
				                userId);
				MaterLevelBlPo materLevelBlPo = new MaterLevelBlPo();
				if (materLevelBlPoList != null && materLevelBlPoList.size() > 0) {
					materLevelBlPo = materLevelBlPoList.get(0);
					BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelBlPo.class, false);
					copier.copy(materLevelPo, materLevelBlPo, null);
					materLevelBlPo.setMaterDay(materDay);
					materLevelBlPo.setMaterValue(materValueNet);
					materLevelBlPo
					        .setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));
					materDaoImpl.updateMaterLevelBl(materLevelBlPo);
				} else {
					BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelBlPo.class, false);
					copier.copy(materLevelPo, materLevelBlPo, null);
					materLevelBlPo.setMaterDay(materDay);
					materLevelBlPo.setMaterValue(materValueNet);
					materLevelBlPo
					        .setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));
					materDaoImpl.addMaterLevelBl(materLevelBlPo);
				}*/

				// 改变所提交日期的实际价格 并生成总价
				List<OrderSupplierMaterRealPo> tOrderSupMaterRealPoList = supplierOrderDao
				        .getOrderSupMaterRealListBySupIdOrdDateMaterIdAndCustId(supId, materDay, materId,
				                userId);

				if (tOrderSupMaterRealPoList != null && tOrderSupMaterRealPoList.size() > 0) {
					for (OrderSupplierMaterRealPo orderSupplierMaterRealPo : tOrderSupMaterRealPoList) {
						orderSupplierMaterRealPo.setOrdMaterValueUnit(materValueUndressed);
						orderSupplierMaterRealPo.setOrdMaterValue(materValueUndressed
						        .multiply(orderSupplierMaterRealPo.getOrdMaterWeight()));
						selectFoodDaoImpl.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("子线程" + i + "结束");
		}

	}
}